﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Kevin.Controls
{
    public class ConvertDataTable2JSON
    {
        /// <summary>
        /// 将DataTable转换为JSON对象
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="dataRows">记录行数</param>
        /// <returns></returns>
        public string CreateJsonParameters(DataTable data,DataTable dataRows)
        {
            /* /****************************************************************************
             * Without goingin to the depth of the functioning
             * of this method, i will try to give an overview
             * As soon as this method gets a DataTable it starts to convert it into JSON String,
             * it takes each row and in each row it grabs the cell name and its data.
             * This kind of JSON is very usefull when developer have to have Column name of the .
             * Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
             * NOTE: One negative point. by this method user
             * will not be able to call any cell by its index.
             * *************************************************************************/

            StringBuilder JsonString = new StringBuilder();

            //Exception Handling
            if (data != null && data.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"Head\":[ ");

                for (int i = 0; i < data.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < data.Columns.Count; j++)
                    {
                        if (j < data.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + data.Columns[j].ColumnName.ToString() +
                                  "\":" + "\"" +
                                  ToHTMLString(data.Rows[i][j].ToString()) + "\",");
                        }
                        else if (j == data.Columns.Count - 1)
                        {
                            JsonString.Append("\"" +
                               data.Columns[j].ColumnName.ToString() + "\":" +
                               "\"" + ToHTMLString(data.Rows[i][j].ToString()) + "\"");
                        }
                    }

                    /*end Of String*/
                    if (i == data.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }

                JsonString.Append("]");
                if (dataRows !=null && dataRows.Rows.Count>0)
                {
                    JsonString.Append(",\"DataRowsCount\":"+"\""+dataRows.Rows[0][0].ToString()+"\"");
                }
                JsonString.Append("}");
                return JsonString.ToString();
            }
            
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 将字符串中的回车换行替换为 &lt;BR>
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        internal  string ToHTMLString(string s)
        {
            StringBuilder buf = new StringBuilder();
            char[] c = s.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                //.Substring(i, 1);
                switch (c[i])
                {
                    case '\n':
                        buf.Append("<br>"); break;
                    case '\r':
                         break;
                    default:
                        buf.Append(c[i]); break;
                }
            }
            return buf.ToString();

        }
    }

    
        
    
}
